#include <iostream>
#include <utils/RefBase.h>

using namespace std;
using namespace android;

class Person : public RefBase {
private:
    wp<Person> father;
    wp<Person> son;

    char *name;
public:
    Person() {
        cout << "Person()" << endl;
    };

    Person(char *name) {
        cout << "Person(char *name)" << endl;
        this->name = name;
    };

    ~Person() {
        cout << "~Person()" << endl;
    }

    void setFather(sp<Person> &father) {
        this->father = father;
    }

    void setSon(sp<Person> &son) {
        this->son = son;
    }

    void printInfo(void) {
        sp<Person> f = father.promote();
        sp<Person> s = son.promote();
        cout << "printInfo: I am " << name << endl;
        if (s != 0)
            cout << "printInfo: My son " << s->name << endl;
        if (f != 0)
            cout << "printInfo: My father " << f->name << endl;
    }
};

void test_func() {
    sp<Person> father = new Person("zhangsan");
    sp<Person> son = new Person("zhangwu");

    father->setSon(son);
    son->setFather(father);

    father->printInfo();
    son->printInfo();
}

int main() {
    test_func();

    return 0;
}
